package com.txby.platformApi.component.web;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.jck.tools.MD5Util;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.DctNrsCardCnf;

/**
 * 医护人员账号管理，开设
 */
@RestController
@RequestMapping("/sys/dctnrs")
public class DctNrsCardCnfController  extends CommonController {
	
	/**
	 * 所有用户列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:dctnrs:list")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT t.*,t2.name deptName FROM  "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" t,sys_dept t2 WHERE t.deptId=t2.dept_id   " ;
		if(getUserId() != Constant.SUPER_ADMIN){
//			params.put("createUserId", getUserId());
			 join=getJoinSqlBracket();
			 sql+=" and t.deptId in "+getJoinSqlBracket();
		}
		String cardNO=params.get("cardNO")==null?"":params.get("cardNO").toString();
		if(!ChkUtil.isEmptyAllObject(cardNO)){
			if (cardNO.contains("%") || cardNO.contains("_")) {
				cardNO = cardNO.trim().replace("%", "\\%").replace("_", "\\_");
				cardNO = "%" + cardNO + "%";
				sql += " and t.cardNO like #{pojo.cardNO} escape '\\'";
				params.put("bedNO", "%" + cardNO.trim() + "%");
			} else {
				cardNO = "%" + cardNO.trim() + "%";
				sql += " and t.cardNO like #{pojo.cardNO} ";
				params.put("cardNO", "%" + cardNO.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by t.updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
 
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:dctnrs:info")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT t.*,t2.name deptName FROM  "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" t,sys_dept t2 WHERE t.deptId=t2.dept_id and t.id_="+id+"" ;
		Map dctnrs =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("dctnrs", dctnrs);
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@RequestMapping("/save")
	@RequiresPermissions("sys:dctnrs:save")
	public R save(@RequestBody DctNrsCardCnf dctnrs){
		try{
		ValidatorUtils.validateEntity(dctnrs);
		String sql="select * from "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" where cardNO='"+dctnrs.getCardNO()+"'";
		DctNrsCardCnf old=(DctNrsCardCnf) this.mBaseDao.findBySqlUniqueObject(sql, DctNrsCardCnf.class, null);
		 if(old!=null){
			 return R.ok().error("卡号重复");
		 }else{
		
		 sql="select * from "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" where jobNO='"+dctnrs.getJobNO()+"'";
		old=(DctNrsCardCnf) this.mBaseDao.findBySqlUniqueObject(sql, DctNrsCardCnf.class, null);
		 if(old!=null){
			 return R.ok().error("工号重复");
		 }
		 }
		 dctnrs.setPassWord(MD5Util.getMD5ofStr(dctnrs.getPassWord(), 2));
		 dctnrs.setCreater(getUserId().toString());
		 dctnrs.setUpdater(getUserId().toString());
			this.mBaseDao.saveObject(dctnrs);
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("sys:dctnrs:update")
	public R update(@RequestBody  DctNrsCardCnf dctnrs){
		try{
		ValidatorUtils.validateEntity(dctnrs);
		   String sql="select * from "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" where cardNO="+dctnrs.getCardNO()+" and id_!="+dctnrs.getId_()+" ";
//		   String sql="select * from "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" where id_="+dctnrs.getId_()+" and id_!="+dctnrs.getId_()+" ";
		DctNrsCardCnf old=(DctNrsCardCnf) this.mBaseDao.findBySqlUniqueObject(sql, DctNrsCardCnf.class, null);
		 if(old!=null){
			 return R.ok().error("卡号 重复");
		 }else{
			 old=(DctNrsCardCnf) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" where   id_="+dctnrs.getId_(), DctNrsCardCnf.class, null);
			 if(old!=null){
				 if(!ChkUtil.isEmpty(dctnrs.getPassWord())){
					 dctnrs.setPassWord(MD5Util.getMD5ofStr(dctnrs.getPassWord(), 2));
				 }
				 this.mBaseDao.updateObject(dctnrs);
			 }
		 }
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:dctnrs:delete")
	public R delete(@RequestBody Long[] ids){
		 for(Long id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(DctNrsCardCnf.class)+" where id_="+id+"", null);
		  }
			return R.ok();
	}
}
